package com.zbkj.crmeb.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.common.CommonPage;
import com.common.PageParamRequest;
import com.constants.Constants;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.crmeb.chant.model.ChannelMerchantAccount;
import com.zbkj.crmeb.chant.service.IChannelMerchantAccountService;
import com.zbkj.crmeb.enums.BusinessCodeEnum;
import com.zbkj.crmeb.enums.NoticeStatusEnum;
import com.zbkj.crmeb.enums.NoticeTypeEnum;
import com.zbkj.crmeb.enums.SwitchEnum;
import com.zbkj.crmeb.store.dao.CustomsRecordDao;
import com.zbkj.crmeb.store.model.CustomsRecord;
import com.zbkj.crmeb.store.request.CustomsRecordSearchRequest;
import com.zbkj.crmeb.store.response.CustomsRecordResponse;
import com.zbkj.crmeb.system.dao.NoticeDao;
import com.zbkj.crmeb.system.model.Notice;
import com.zbkj.crmeb.system.request.NoticeSearchRequest;
import com.zbkj.crmeb.system.response.NoticeResponse;
import com.zbkj.crmeb.system.service.NoticeService;
import com.zbkj.crmeb.system.service.SystemAdminService;

@Slf4j
@Service("noticeService")
public class NoticeServiceImpl extends ServiceImpl<NoticeDao, Notice> implements NoticeService {
    @Resource
    private NoticeDao noticeDao;
    @Autowired
    private SystemAdminService systemAdminService;
    @Autowired
    private IChannelMerchantAccountService channelMerchantAccountService;

    /**
     * 列表
     * @param request 请求参数
     * @author Mr.Zhang
     * @since 2020-05-27
     * @return List<StoreProduct>
     */
    @Override
    public PageInfo<NoticeResponse> getList(NoticeSearchRequest request) {
        //条件查询
        Page<Notice> noticePage = PageHelper.startPage(request.getPage(), request.getLimit());
        List<NoticeResponse> notices = noticeDao.queryNoticeList(request);
        if(CollectionUtils.isEmpty(notices)) {
            return CommonPage.copyPageInfo(noticePage, new ArrayList<>());
        }
//        List<NoticeResponse> noticeResponses = notices.stream().map(item -> {
//            NoticeResponse response = new NoticeResponse();
//            BeanUtils.copyProperties(item, response);
//            if(!StringUtils.isEmpty(request.getKeywords())){
//                response.setKeywords(request.getKeywords());
//            }
//            return response;
//        }).collect(Collectors.toList());
        return CommonPage.copyPageInfo(noticePage, notices);
    }

    /**
     * 新增
     *
     * @param notice
     * @return
     */
    @Override
    public boolean add(Notice notice) {
        log.info("进入notice add方法：{}", JSON.toJSONString(notice));

        if(notice.getAdminId() == null){
            try {
                int adminId = systemAdminService.getAdminId();
                notice.setAdminId(adminId);
            }catch (Exception e){
                log.error("获取当前用户id报错：{}", e.getMessage());
            }
        }
        notice.setNoticeStatus(NoticeStatusEnum.NEW.getCode());
        if(notice.getBusinessCode() == null) {
            notice.setBusinessCode(BusinessCodeEnum.MERCHANT_ACCOUNT_WARN.getCode());
            notice.setBusinessName(BusinessCodeEnum.MERCHANT_ACCOUNT_WARN.getMsg());
        }
        notice.setIsDel(SwitchEnum.CLOSE.getCode());
        if(notice.getNoticeType() == null) {
            notice.setNoticeType(NoticeTypeEnum.BOTTOM_RIGHT.getCode());
        }
        if(null == notice.getContent()){
            Integer merId = notice.getMerId();
            ChannelMerchantAccount channelMerchantAccount = channelMerchantAccountService.getOne(Wrappers.<ChannelMerchantAccount>lambdaQuery().eq(ChannelMerchantAccount :: getMerId, merId));
            log.info("notice channelMerchantAccount ：{}", JSON.toJSONString(channelMerchantAccount));

            // 金额校验
            if(null != channelMerchantAccount && null != channelMerchantAccount.getAccount()) {
                //余额支出 且 余额小于预警金额
                if(channelMerchantAccount.getAccount().compareTo(Constants.FIVE_THOUSAND_BIGDECIMAL)<0){
                    notice.setTitle(BusinessCodeEnum.MERCHANT_ACCOUNT_WARN.getMsg());
                    notice.setContent("您的账户余额所剩不多，现有余额为"+channelMerchantAccount.getAccount()+"元，为了不影响您的后续业务，请及时充值！");
                }

            }
        }
        log.info("notice：{}", JSON.toJSONString(notice));

        return this.save(notice);
    }

}